<script setup lang="tsx">
import { NButton, NTag } from 'naive-ui';
import { fetchGetLogList } from '@/service/api';
import { $t } from '@/locales';
import { useAppStore } from '@/store/modules/app';
import { useTable, useTableOperate } from '@/hooks/common/table';
import ShowDetailModal from './modules/show-detail-modal.vue';
import LogSearch from './modules/poem-search.vue';

const appStore = useAppStore();

const { columns, columnChecks, data, getData, loading, mobilePagination, searchParams, resetSearchParams } = useTable({
  apiFn: fetchGetLogList as any,
  showTotal: true,
  apiParams: {
    current: 1,
    size: 10
    // if you want to use the searchParams in Form, you need to define the following properties, and the value is null
    // the value can not be undefined, otherwise the property in Form will not be reactive
  },
  columns: () => [
    // {
    //   type: 'selection',
    //   align: 'center',
    //   width: 48
    // },
    {
      key: 'index',
      title: $t('common.index'),
      align: 'center',
      width: 48
    },
    {
      key: 'id' as any,
      title: $t('page.manage.poem.id'),
      align: 'center',
      minWidth: 60,
      width: 90
    },
    {
      key: 'poemTitle' as any,
      title: $t('page.manage.poem.poemTitle'),
      align: 'center',
      minWidth: 60,
      width: 90
    },
    {
      key: 'author' as any,
      title: $t('page.manage.poem.author'),
      align: 'center',
      minWidth: 60,
      width: 90
    },
    {
      key: 'dynasty' as any,
      title: $t('page.manage.poem.dynasty'),
      align: 'center',
      minWidth: 60,
      width: 90
    },
    {
      key: 'content' as any,
      title: $t('page.manage.poem.content'),
      align: 'center',
      minWidth: 60,
      width: 90,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'description' as any,
      title: $t('page.manage.poem.description'),
      align: 'center',
      minWidth: 60,
      width: 90,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'style' as any,
      title: $t('page.manage.poem.style'),
      align: 'center',
      minWidth: 60,
      width: 90
    },
    {
      key: 'tag' as any,
      title: $t('page.manage.poem.tag'),
      align: 'center',
      minWidth: 60,
      width: 90
    },
    /*    {
      key: 'gender',
      title: $t('page.manage.log.ip'),
      align: 'center',
      width: 60,
      render: row => {
        if (row.gender === null) {
          return null;
        }

        const tagMap: Record<Api.SystemManage.gender, NaiveUI.ThemeColor> = {
          1: 'primary',
          0: 'error'
        };

        const label = $t(genderRecord[row.gender]);

        return <NTag type={tagMap[row.gender]}>{label}</NTag>;
      }
    }, */

    /*    {
      key: 'status',
      title: $t('page.manage.log.status'),
      align: 'center',
      width: 60,
      render: row => {
        if (row.status === null) {
          return null;
        }

        const tagMap: Record<Api.Common.EnableStatus, NaiveUI.ThemeColor> = {
          1: 'success',
          0: 'warning'
        };

        const label = $t(enableStatusRecord[row.status]);

        return <NTag type={tagMap[row.status]}>{label}</NTag>;
      }
    }, */
    /*    {
      key: 'status',
      title: $t('page.manage.log.status'),
      align: 'center',
      width: 60,
      render: row => {
        if (row.status === null) {
          return null;
        }

        const switchMap: Record<Api.Common.EnableStatus, Boolean> = {
          '1': true,
          '0': false
        };
        return <n-switch  v-model:value={switchMap[row.status]} disabled />;
      }
    }, */

    {
      key: 'operate',
      title: $t('common.operate'),
      align: 'center',
      width: 130,
      render: row => (
        <div class="flex-center gap-8px">
          <NButton type="primary" ghost size="small" onClick={() => edit((row as any).id)}>
            {$t('common.edit')}
          </NButton>

          <NButton type="info" ghost size="small" onClick={() => showDetail((row as any).id)}>
            {$t('common.detail')}
          </NButton>
        </div>
      )
    }
  ]
});

const { modalVisible, operateType, modalFormData, checkedRowKeys, handleAdd, handleEdit, onShowDetail } =
  useTableOperate(data as any, getData);

// 打开详情 弹窗
function showDetail(id: number) {
  onShowDetail(id);
}

function edit(id: number) {
  handleEdit(id);
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <LogSearch v-model:model="searchParams" @reset="resetSearchParams" @search="getData" />
    <NCard :title="$t('page.manage.log.title')" :bordered="false" size="small" class="sm:flex-1-hidden card-wrapper">
      <template #header-extra>
        <TableHeaderOperation
          v-model:columns="columnChecks"
          :disabled-delete="true"
          :show-delete="false"
          :loading="loading"
          @add="handleAdd"
          @refresh="getData"
        />
      </template>
      <NDataTable
        v-model:checked-row-keys="checkedRowKeys"
        :columns="columns"
        :data="data as any"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="962"
        :loading="loading"
        remote
        :row-key="(row: any) => row.id"
        :single-line="false"
        :pagination="mobilePagination"
        class="sm:h-full"
      />

      <ShowDetailModal v-model:visible="modalVisible" :row-data="modalFormData as any" />
    </NCard>
  </div>
</template>

<style scoped></style>
